package com.example.mynewsweb.controller;

import com.example.mynewsweb.po.Comment;
import com.example.mynewsweb.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Controller
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;

    /*发布评论*/
    @PostMapping("api/addComment")
    public Map<String,Object> addComment(@RequestBody Comment comment, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        try{
            //发布评论的时间
            Date date=new Date();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            comment.setTime(dateFormat.format(date));
            //发布评论
            commentService.addComment(comment);
            map.put("state",true);
            map.put("msg","发布评论成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","发布评论失败");
        }
        return map;
    }

    /*显示当前新闻的评论*/
    @ResponseBody
    @GetMapping("api/showComment")
    public List<Comment> showComment(String newId){
        return commentService.showComment(newId);
    }


}
